package com.iot.ht.controller.htcls;

import com.iot.core.annotation.Auth;
import com.iot.core.annotation.Log;
import com.iot.core.data.BasePageDtQto;
import com.iot.core.data.IdNameDto;
import com.iot.core.data.entity.HtCls;
import com.iot.core.data.mapper.HtClsMapper;
import com.iot.core.enums.AuthType;
import com.mybatisflex.core.paginate.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.InvocationTargetException;

import static com.iot.core.data.entity.table.HtActionLogTableDef.HT_ACTION_LOG;
import static com.iot.core.data.entity.table.HtClsTableDef.HT_CLS;

@Slf4j
@RestController
@RequestMapping("/htcls")
@Tag(name = "类型管理", description = "")
public class HtClsController {
    @Autowired
    HtClsMapper htClsMapper;

    @Auth(authType = AuthType.Login)
    @Log(logNeed = false,logTemplete ="",logPin = true,logPout = true)
    @PostMapping("/page")
    @Operation(summary = "类型分页", description = "")
    public Page<HtCls> page(@RequestBody BasePageDtQto qm) throws Exception {
        var qw=htClsMapper.getQueryWrapper();
        if(StringUtils.hasText(qm.getDtb() )){
            qw.and( HT_CLS.EDT_DT.ge(qm.getDB()));
        }

        if(StringUtils.hasText(qm.getDte() )){
            qw.and( HT_CLS.EDT_DT.lt(qm.getDE()));
        }

        if(StringUtils.hasText(qm.getKey() )){
            if(StringUtils.hasText(qm.getKey() )){
                qw.and( HT_CLS.FULL_NAME.like(qm.getKey())

                );
            }
        }

        var sort=qm.getSort();
        qw.orderBy( sort.toString()  );

        var r=htClsMapper.page(  qm,qw );
        return r;
    }


    @Auth(authType = AuthType.Login)
    @Log(logNeed = false,logTemplete ="",logPin = true,logPout = true)
    @PostMapping("/newobj")
    @Operation(summary = "根据类名创建对象", description = "")
    public Object NewObj(@RequestBody IdNameDto qm) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {

        Class<?> clazz = Class.forName(qm.getName());
        Object r= clazz.getDeclaredConstructor().newInstance();
        return r;
    }
}
